package Application.UIEditApplication.view.ui
{
	import Application.UIEditApplication.def.Def;
	import Application.UIEditApplication.def.Manager;
	import Application.UIEditApplication.view.ui.cell.ComponentListCell;
	
	import XComponent.XComponents;
	import XComponent.hide.XEvent;
	import XComponent.show.XFrame;
	import XComponent.show.XLoader;
	
	import def.XG;
	
	import fl.controls.Button;
	import fl.controls.ComboBox;
	import fl.controls.List;
	import fl.core.UIComponent;
	import fl.data.DataProvider;
	
	import flash.display.Bitmap;
	import flash.display.BitmapData;
	import flash.display.DisplayObject;
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.utils.getDefinitionByName;

	public class ComponentListMed 	extends UIBaseMed
	{
		static public const NAME : String = "ComponentListMed";
		
		private var m_componentList : List;
		
		private var m_dpComonentList : DataProvider;
		
		public function ComponentListMed()
		{
			super(NAME,[Def.STAGE_ALIGN_LEFT,Def.STAGE_ALIGN_TOP]);
		}
		
		override public function onRegister():void
		{
			m_componentList = new List;
			m_dpComonentList = new DataProvider;
			
			m_componentList.dataProvider = m_dpComonentList;
			
			m_componentList.setStyle("cellRenderer",ComponentListCell);
			
			setComponentData(Manager.XML.Component.DataToArray);
			
			m_componentList.width = 125;
			
			setOffset(0,30);
			
			Manager.Mouse.startMove();
			Manager.Mouse.setHandBoxOffset(1,1);
			
			var pan : Sprite = this.viewComponent as Sprite;
			
			pan.addChild(m_componentList);
			
			m_componentList.addEventListener(XEvent.XEVENT_SELECT,OnClickComponentListCell);
		}
		
		override public function onRemove():void
		{
			m_componentList.removeEventListener(XEvent.XEVENT_SELECT,OnClickComponentListCell);
			
			m_componentList = 			null;
			m_dpComonentList =		 	null;
	
		}
		
		private function OnClickComponentListCell(evt : XEvent) : void
		{
			var cellData : Object = evt.data;
			
			if(cellData)
			{
				var cla : Class;
				
				var className : String = cellData["id"];
				
				switch(className)
				{
					default : 
						cla = XComponents.component(className);	
					break;
				}
				
				if(cla)
				{
					var hand : * = new cla ;
					
					if(hand is XLoader)
					{
						hand.load();
					}

					XG.showMouseHand(hand);
					
					sendNotification(Def.MOUSE_HANDBOXCLASSCHANGED,cla);
				}
			}
		}
		
		public function setComponentData(listData:Array) : void
		{
			m_dpComonentList.removeAll();
			
			if(!listData) return;
			
			m_dpComonentList.addItems(listData);
			m_componentList.rowHeight = 18;
			m_componentList.height = m_componentList.rowHeight*listData.length;
		}
	}
}